package com.hf.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import tool.ClientTool;
import tool.StringTool;

import com.base.bean.*;
import com.base.dao.*;
import com.base.domain.ClientDetail;
import com.base.service.*;
import com.hf.bean.CheckClientBean;
import com.hf.bean.SimpleAddress;

public class ClientAction3 extends BaseAction {
	private ClientService cs = ClientService.getInstance(); // 私有无geter seter

	private String phones, addNames, addCodes; // 分别以"|"分割
	private Integer cid; // 客户id值，在修改的时候会传入
	private String name, comName, memo;
	private String[] chose1, chose2, chose3; // 多选
	private String chose4, chose5; // 单选
	private Integer chose6;			//单选
	private AddressService as = AddressService.getInstance();

	public ClientAction3() {
		
	}

	// 新增或者编辑客户
	public String merge() throws Exception {
 	// HttpServletRequest request=ServletActionContext.getRequest();
	// HttpSession session=request.getSession();
	// User user=(User)session.getAttribute("user"); //正式用替换下面这句
	   User user = new User(new Integer(1));

//		this.phones="123|223|323";
//		System.out.println("phones:"+phones);
		if(name==null||phones==null)
			return SUCCESS;
		
		CheckClientBean checkClient=new CheckClientBean();
		checkClient.setCid(cid);
		checkClient.setName(name);
		checkClient.setPhones(phones.split("\\|"));
		checkClient=checkClient.check(checkClient, user.getUid());
		
		
		if(checkClient.getResultType()!=1){			//验证未能通过 直接返回	
			return SUCCESS;
		}
		
		this.tip=checkClient.getCheckResult();	//输出提示
		if (true)
			return SUCCESS;
		
		
		
		boolean editMode = false;	
		boolean isAddSuccess=false;
		boolean isEditSuccess=false;
		if (cid != null) {
			editMode = true; 	// 此为编辑模式（非新增）
		}
//		if (true)				//设置断点
//			return SUCCESS;

		if (!check()) 			// 验证不通过（通过tip可得知验证不通过的原因）
			return SUCCESS; 

		if(editMode)			//编辑模式
			editClient();
		else
			addClient();		//新增模式
		
		return SUCCESS;
	}

	@Override
	public String execute() {
		// TODO Auto-generated method stub
		return SUCCESS;
	}

	//#####################################################################################################
	
	//#####################################################################################################
	
	
	
	/**
	 * 编辑模式
	 * 待完善：欲购机时间是否更改
	 */
	private boolean editClient() {
		boolean addIsNotChange = false;
		boolean isOwn = false;	// 是否是自己的客户，默认为不是，如果不是自己的客户只能修改扩展信息，参考ClientService.editClient
		boolean isSellPanChange = false;	// 欲购机时间是否更改
		// HttpServletRequest request=ServletActionContext.getRequest();
		// HttpSession session=request.getSession();
		// User user=(User)session.getAttribute("user"); //正式用替换下面这句
		User user = new User(new Integer(1));

		if (addCodes.equals("0|0|0")) {
			addIsNotChange = true; 				// 这种情况，不需要更新省市区
		}
		ClientBase cBase = cs.getClientBaseDao().findByHql("from ClientBase c where c.cid="+cid).get(0);	
		if(cBase.getCreator().equals(user.getUid()))
			isOwn = true;			//是自己的客户
		
//		cBase.setCid(cid);		//多余
		cBase.setName(name);
		cBase.setComName(comName);
		
		if(addIsNotChange == false)	{	
			String[] _addCodes = addCodes.split("\\|");
			if (_addCodes[0] != "" && _addCodes[0] != null)
				cBase.setProvinceId(_addCodes[0]);
			if (_addCodes[1] != "" && _addCodes[1] != null)
				cBase.setCityId(_addCodes[1]);
			if (_addCodes[2] != "" && _addCodes[2] != null)
				cBase.setAreaId(_addCodes[2]);
		}
		
		ClientExtend cExtend = cs.getClientExtendDao().findByHql("from ClientExtend e where e.id.cid="+cid+" and e.id.creator="+user.getUid()).get(0);
		cExtend.setId(new ClientExtendId(cid,user.getUid()));
		cExtend.setMemo(memo.trim());
		cExtend.setYywjj(StringTool.arrToStr(chose1));
		cExtend.setYyzzj(StringTool.arrToStr(chose2));
		cExtend.setYgsbType(StringTool.arrToStr(chose3));
		cExtend.setClientType(chose4);
		cExtend.setIsZan(chose5);
		cExtend.setGjTimeType(chose6);

		
		cs.editClient(isOwn, cBase, cExtend, phones);
		this.tip=tip+"修改成功";
		
		return true;
	}

	// 私有方法
	private boolean addClient() {
		// 新增客户
		// HttpServletRequest request=ServletActionContext.getRequest();
		// HttpSession session=request.getSession();
		// User user=(User)session.getAttribute("user"); //正式用替换下面这句
		User user = new User(new Integer(1));

		memo = (memo != null) && (memo.trim() != "") ? memo : "";
		String[] _addCodes = addCodes.split("\\|");
		ClientBase cBase = new ClientBase();
		cBase.setOwner(user.getUid());
		cBase.setName(name);
		cBase.setComName(comName);
		if (_addCodes[0] != "" && _addCodes[0] != null)
			cBase.setProvinceId(_addCodes[0]);
		if (_addCodes[1] != "" && _addCodes[1] != null)
			cBase.setCityId(_addCodes[1]);
		if (_addCodes[2] != "" && _addCodes[2] != null)
			cBase.setAreaId(_addCodes[2]);

		ClientExtend cExtend = new ClientExtend();
		cExtend.setId(new ClientExtendId(new Integer(0), user.getUid()));
		cExtend.setMemo(memo.trim());
		cExtend.setYywjj(StringTool.arrToStr(chose1));
		cExtend.setYyzzj(StringTool.arrToStr(chose2));
		cExtend.setYgsbType(StringTool.arrToStr(chose3));
		cExtend.setClientType(chose4);
		cExtend.setIsZan(chose5);
		cExtend.setGjTimeType(chose6);

		this.setCid(cs.addClient(cBase, cExtend, phones)); // 添加成功返回该cid值
		this.tip=tip+"添加成功";

		return true;
	}

	// 私有方法：验证电话号码以及姓名
	private boolean check() throws Exception { // 检查电话号码以及姓名
		this.tip="";
		name = (name != null) && (name.trim() != "") ? name : ""; // name去空格
		String[] _phones = this.phones.split("\\|");

		if (ClientTool.isName(this.name) != 0) {
			this.tip =tip+ "请检查姓名是否有非法字符";
			return false;
		}
		System.out.println("phones:" + phones); // 正式删除

		String _phone = phones; // 只有一个电话的情况
		if (_phone != null && _phone != "") {
			int i = cs.checkPhone(_phone);
			if (i == 1) {
				System.out.println("_iphone:" + _phone); // 正式删除
				this.tip =tip+ "电话格式错误";
				return false;
			} else if (i == 2) {
				this.tip = tip+"电话号码重复";
				return false;
			}
		}

		this.tip = tip+"验证通过";
		return true;
	}

	public String getPhones() {
		return phones;
	}

	public void setPhones(String phones) {
		this.phones = phones;
	}

	public String getAddNames() {
		return addNames;
	}

	public void setAddNames(String addNames) {
		this.addNames = addNames;
	}

	public String getAddCodes() {
		return addCodes;
	}

	public void setAddCodes(String addCodes) {
		this.addCodes = addCodes;
	}

	public Integer getCid() {
		return cid;
	}

	public void setCid(Integer cid) {
		this.cid = cid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getComName() {
		return comName;
	}

	public void setComName(String comName) {
		this.comName = comName;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public String[] getChose1() {
		return chose1;
	}

	public void setChose1(String[] chose1) {
		this.chose1 = chose1;
	}

	public String[] getChose2() {
		return chose2;
	}

	public void setChose2(String[] chose2) {
		this.chose2 = chose2;
	}

	public String[] getChose3() {
		return chose3;
	}

	public void setChose3(String[] chose3) {
		this.chose3 = chose3;
	}

	public String getChose4() {
		return chose4;
	}

	public void setChose4(String chose4) {
		this.chose4 = chose4;
	}

	public String getChose5() {
		return chose5;
	}

	public void setChose5(String chose5) {
		this.chose5 = chose5;
	}

	public Integer getChose6() {
		return chose6;
	}

	public void setChose6(Integer chose6) {
		this.chose6 = chose6;
	}

	

}
